package com.platform.usercenter.controller;

import com.platform.common.core.domain.entity.SysDept;
import com.platform.common.core.utils.StringUtils;
import com.platform.common.core.web.controller.BaseController;
import com.platform.common.core.web.domain.AjaxResult;
import com.platform.common.security.annotation.RequiresPermissions;
import com.platform.common.security.utils.SecurityUtils;
import com.platform.usercenter.domain.SysOrganizationDept;
import com.platform.usercenter.service.ISysDeptService;
import com.platform.usercenter.service.ISysOrganizationDeptService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门管理
 *
 * @author: wcs
 * @date: 2025-04-14 17:17:57
 **/
@RestController
@RequestMapping("/dept")
public class SysDeptController extends BaseController {
    @Resource
    private ISysDeptService deptService;
    @Resource
    private ISysOrganizationDeptService organizationDeptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public AjaxResult list(SysOrganizationDept dept) {
        List<SysOrganizationDept> deptList = organizationDeptService.selectSysOrganizationDeptList(dept);
        return success(deptList);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
        List<SysDept> depts = deptService.selectDeptList(new SysDept());
        depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return success(depts);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable Long deptId) {
        return success(organizationDeptService.selectSysOrganizationDeptByDeptId(deptId));
    }

    /**
     * 新增部门
     */
    @RequiresPermissions("system:dept:add")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysOrganizationDept dept) {
        dept.setCreateBy(SecurityUtils.getUsername());
        return toAjax(organizationDeptService.insertSysOrganizationDept(dept));
    }

    /**
     * 修改部门
     */
    @RequiresPermissions("system:dept:edit")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysOrganizationDept dept) {
        dept.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(organizationDeptService.updateSysOrganizationDept(dept));
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("system:dept:remove")
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable Long deptId) {
        if (deptService.hasChildByDeptId(deptId)) {
            return warn("存在下级部门,不允许删除");
        }
        if (deptService.checkDeptExistUser(deptId)) {
            return warn("部门存在用户,不允许删除");
        }
        deptService.checkDeptDataScope(deptId);
        return toAjax(organizationDeptService.deleteSysOrganizationDeptByDeptId(deptId));
    }


    /***
     *  部门 id 获取部门 信息
     * @param deptId
     * @return
     */
    @GetMapping("/getDeptById/{deptId}")
    public AjaxResult getDeptById(@PathVariable Long deptId) {
        return AjaxResult.success(organizationDeptService.selectSysOrganizationDeptByDeptId(deptId));
    }
}
